package com.rc.saas.tenant.controller.tenant;

import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.tenant.TenantOrganization;
import com.rc.saas.tenant.model.tenant.TenantOrganizationExample;
import com.rc.saas.tenant.service.tenant.TenantOrgService;
import com.rc.saas.tenant.vo.tenant.TreeModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by sven on 2018-05-17
 */
@Controller
@RequestMapping("/tenant/org")
public class OrgController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(OrgController.class);

    private String prefix = "/templates/tenant/org";

    @Autowired
    private TenantOrgService tenantOrgService;

    /**
     * 跳转首页列表
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        return prefix + "/org";
    }

    /**
     * 返回列表数据
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<TenantOrganization> list(TenantOrganization organization) {
        TenantOrganizationExample example = new TenantOrganizationExample();
        //example.setOrderByClause("sort ASC");
        List<TenantOrganization> orgs = tenantOrgService.selectByExample(example);
        return orgs;
    }

    /**
     * 跳转到增加页面
     *
     * @param parentId
     * @param model
     * @return
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, Model model) {
        model.addAttribute("parentId", parentId);
        if (parentId == null || parentId == 0) {
            model.addAttribute("parentName", "根目录");
        } else {
            model.addAttribute("parentName", tenantOrgService.selectByPrimaryKey(parentId).getOrgName());
        }
        return prefix + "/addOrg";
    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable("id") Long id, Model model) {
        TenantOrganization organization = tenantOrgService.selectByPrimaryKey(id);
        Long parentId = organization.getParentId();
        model.addAttribute("parentId", parentId);
        if (parentId == null || parentId == 0) {
            model.addAttribute("parentName", "根目录");
        } else {
            model.addAttribute("parentName", tenantOrgService.selectByPrimaryKey(parentId).getOrgName());
        }

        model.addAttribute("organization", organization);
        return prefix + "/editOrg";
    }

    /**
     * 新增保存
     *
     * @param request
     * @param organization
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public TResponse save(HttpServletRequest request, TenantOrganization organization) {
        tenantOrgService.insertSelective(organization);
        return TResponse.SUCCESS();
    }

    /**
     * 修改保存
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public TResponse update(HttpServletRequest request, TenantOrganization organization) {
        tenantOrgService.updateByPrimaryKey(organization);
        return TResponse.SUCCESS();
    }

    /**
     * 删除
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    public TResponse remove(HttpServletRequest request, Long id) {
        try {
            tenantOrgService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            return TResponse.FAIL("操作失败");
        }

        return TResponse.SUCCESS();
    }

    /**
     * 打开组织页面
     *
     * @return
     */
    @RequestMapping(value = "/openTreeView", method = RequestMethod.GET)
    public String openTreeView() {
        return prefix + "/orgTree";
    }

    /**
     * 组织树数据
     *
     * @return
     */
    @RequestMapping(value = "/orgTreeData", method = RequestMethod.GET)
    @ResponseBody
    public TreeModel<TenantOrganization> orgTreeData() {
        //if (getSessionTenantUser() == null) {
        //    return null;
        //} else {
        return tenantOrgService.buildOrgTree();
        //}
    }

    /**
     * 打开组织用户页面
     *
     * @return
     */
    @RequestMapping(value = "/openTreeUserView", method = RequestMethod.GET)
    public String openTreeUserView(Integer type, Model model) {
        model.addAttribute("type", type);
        return prefix + "/orgUserTree";
    }

    /**
     * 组织用户树数据
     *
     * @return
     */
    @RequestMapping(value = "/orgTreeUserData", method = RequestMethod.GET)
    @ResponseBody
    public TreeModel<TenantOrganization> orgTreeUserData() {
        //if (getSessionTenantUser() == null) {
        //    return null;
        //} else {
        return tenantOrgService.buildOrgUserTree();
        //}
    }
}
